<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>查询班级教具信息</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div class="applyTitle mb-10">
          <el-select v-model="editForm.section_id" placeholder="年段" @change="sectionChange" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.section_name" :value="item.id" v-for="(item, index) in sectionList"
                       :key="index"></el-option>
          </el-select>
          <el-select v-model="editForm.class_id" placeholder="班级" size="small" class="w-140 mr-10">
            <el-option :label="item.class_name" :value="item.id" v-for="(item, index) in classesList"
                       :key="item.id"></el-option>
          </el-select>
          <el-button type="primary" size="small" @click="searchFn('search')"><i class="el-icon-search"></i>查询</el-button>
          <el-button size="small" @click="searchFn('refresh')"><i class="el-icon-refresh"></i>重置刷新</el-button>
          <el-button size="small" disabled v-if="!editForm.class_id">班级教具查询详情导出</el-button>
          <downexcel type="teachingClassSearch" :option="editForm" v-else></downexcel>
        </div>
        <el-table
            ref="multipleTable"
            :data="arryList"
            :stripe="tableStripe"
            :border="tableBorder"
            :height="tableHeight"
            tooltip-effect="dark"
            header-cell-class-name="table-text-c"
            cell-class-name="table-text-c">
          <el-table-column
              label="教具名称"
              align="center">
            <template slot-scope="scope">
              {{scope.row.tools.name}}
            </template>
          </el-table-column>
          <el-table-column
              prop="num"
              label="教具类目"
              align="center">
            <template slot-scope="scope">
              {{scope.row.tools.cate?scope.row.tools.cate.name:'未知'}}
            </template>
          </el-table-column>
          <el-table-column
              prop="num"
              label="数量"
              width="120"
              align="center">
          </el-table-column>
        </el-table>
        <!--工具条 分页-->
        <el-col :span="24" class="toolbar">
          <el-pagination
              background
              :page-size="page.pagesize"
              :current-page="page.pageindex"
              layout="total, prev, pager, next"
              :total="page.total"
              @current-change="handleSizeChange"></el-pagination>
        </el-col>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small" >确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {pagination} from '@/utils/util.js'

export default {
  props: ['visible','detailOption'], // commoent 学生点评 collection 学生采集
  components: {
  },
  data() {
    return {
      dialogVisible: false,
      tableStripe: true, // 是否斑马栏
      tableBorder: true, // 单元格右边边框
      arryList: [], // 数据列表
      // 页数
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      editForm: {
        section_id: '',
        class_id: ''
      },
      sectionList: [],
      classesList: [],
      tableHeight: 0
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if(this.visible){
        let n =this;
        this.$nextTick(()=>{
          let height= n.$refs.dialogBox.offsetHeight;  //top 50 1 search 50 page 50 10 footer 50 1
          this.tableHeight = height - (51+40+42+60+51)
          console.log('height:', height)
        })
        this.classList()
      }else{
        this.refreshFn();
      }
    }
  },
  created() {
  },
  methods: {
    searchFn (option) {
      if(option === 'search'){
        console.log('搜索内容信息：', option)
        if (!this.editForm.class_id) {
          this.$message.error('请选择搜索教具得班级')
          return
        }
      }else{
        this.refreshFn();
        return
      }
      this.$fetch(this.api.tearch.search, {
        class_id: this.editForm.class_id,
        p: this.page.pageindex
      }).then((response) => {
        console.log('数据：', response)
        if (response.code === 0) {
          this.arryList = response.list.data
          this.page.total = response.list.total
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    refreshFn(){
      this.editForm.section_id = '';
      this.editForm.class_id = '';
      this.page.pageindex = 1;
      this.page.total = 0;
      this.arryList = [];
    },
    classList () {
      // 年段
      this.$fetch(this.api.user_sectionindex, {p: this.page.pageindex}).then((response) => {
        if (response.code === 0) {
          this.sectionList = response.list.data
          this.classSecondList()
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    classSecondList () {
      // 班级
      this.$fetch(this.api.user_classesindex, {p: this.page.pageindex}).then((response) => {
        if (response.code === 0) {
          let array_ = {}
          for (let i in response.list.data) {
            let new_array = array_[response.list.data[i].section_id] || []
            new_array.push(response.list.data[i])
            array_[response.list.data[i].section_id] = new_array
          }
          this.classesListNew = array_
          console.log('班级信息:', array_)
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    sectionChange (val) {
      for (let i in this.sectionList) {
        if (this.sectionList[i].id === val) {
          this.classesList = this.classesListNew[this.sectionList[i].section_name]
          return
        }
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option){
      return JSON.parse(JSON.stringify(option))
    },
    // 分页
    handleSizeChange (val) {
      this.page.pageindex = val
      this.searchFn('search')
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 80%;
  height: 75%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding:20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.toolbarLine {
  text-align: center;
}
.hearder-search{
  display: flex;
  align-items: center;
}
</style>


